#pragma once
#include "InetAddress.h"
#include "NonCopyable.h"
#include "Channel.h"
#include "Socket.h"
#include <functional>

class EventLoop;
class Acceptor : NonCopyable
{
public:
    typedef std::function<void(int sockfd, const InetAddress&)> NewConnectionCallback;
    Acceptor(EventLoop *loop, const InetAddress &addr);
    ~Acceptor() {}

    void setNewConnectionCallback(const NewConnectionCallback &cb)
    {
        _newConnectionCallback = cb;
    }

    bool listening() const { return _listening; }
    void listen();

private:
    void handleRead(); // called when new connection is available

    void setReuseAddr();
    void setReusePort();
    void bind();

    EventLoop *_loop;
    InetAddress _addr;
    Socket _acceptSocket; // listen socket
    Channel _acceptChannel;
    NewConnectionCallback _newConnectionCallback;
    bool _listening;

};

